<template>
  <div class="course-rule">
    <!-- 顶部查询 -->
      <rule-manager-tab status="1" view-model="1"></rule-manager-tab>
  </div>
</template>

<script>
import course from '@/api/course.js'
const ruleManagerTab = () => import('@/views/course/rule/RuleManagerTab.vue')
export default {
  components: {
      ruleManagerTab
  },
  // 保存时key ruleList
  name: 'courseRule',
  props: {
    editId: String,
    editData: Object
  },
  data() {
    return {
      groupList: [],
      selectedData: [],
      allData: [],
      search: {
        name: '',
        group: ''
      },
      leftSearch: {
        name: '',
        group: ''
      },
      rightSearch: {
        name: '',
        group: ''
      },
    }
  },
  computed: {
    leftGroup() {
      let arr = []
      let allData =
        this.leftSearch.name || this.leftSearch.group
          ? this.allData.filter(k => {
              if (this.leftSearch.name) {
                return k.mc.includes(this.leftSearch.name)
              }
              if (this.leftSearch.group) {
                return k.fz.includes(this.leftSearch.group)
              }
            })
          : this.allData
      allData.forEach(k => {
        let index = arr.findIndex(j => {
          return j.groupName == k.fz
        })
        if (index == -1) {
          arr.push({
            groupName: k.fz,
            children: [k]
          })
        } else {
          arr[index].children.push(k)
        }
      })
      return arr
    },
  },
  created() {
    this.initData()
  },
  methods: {
    getGroupName(key) {
      if (!key) {
        return
      }
      let obj = this.groupList.find(k => {
        return k.key == key
      })
      return obj ? obj.value : key
    },
    handleSearch(type) {
      if (type == 'left') {
        this.leftSearch = {
          name: this.search.name,
          group: this.search.group
        }
      } else {
        this.rightSearch = {
          name: this.search.name,
          group: this.search.group
        }
      }
    },
    initData() {
      Promise.all([
        course.getRuleList({
          pageBean: {page: 1, pageSize: -1, total: 0},
          sorter: [],
          querys:[{
              property: 'zt',
              value: '1',
              group: 'main',
              operation: 'EQUAL',
              relation: 'AND'}]
        }),
        course.getRuleGroupList()
      ]).then(resultArr => {
        let res = resultArr[0]
        this.groupList = resultArr[1]
        if (res.rows) {
            this.allData = res.rows
        }
      })
    },
    selectLeftItem(item) {
      if (item.disabled) {
        return
      }
      this.$set(item, 'selected', !item.selected)
    },
    reset() {
      this.search = {
        name: '',
        group: ''
      }
      this.leftSearch = {
        name: '',
        group: ''
      }
      this.rightSearch = {
        name: '',
        group: ''
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.course-rule {
  padding: 24px;
  .search-bar {
    display: flex;
    align-items: center;
    width: calc(80% + 50px);
    margin: 0 auto;
    label {
    }
    .search-bar-input {
      margin-right: 24px;
    }
  }
  .course-rule_main {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px 0;
    // width: 1200px;
    .main-left,
    .main-right {
      width: 40%;
      .common-title {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 8px;
        span {
          font-size: 16px;
          color: #333;
          font-weight: bold;
        }
      }
      .scroll-box {
        border: 1px solid $project-border-color;
        width: 100%;
        height: 500px;
        overflow-y: auto;
        padding: 16px;
        box-sizing: border-box;
        .scroll-box_group {
          margin-bottom: 16px;
        }
        .scroll-box_item {
          padding-left: 16px;
          line-height: 24px;
          cursor: pointer;
          &:hover:not(.disabled),
          &.selected {
            background: #f0f8ff;
          }
          &.selected {
            color: $project-color-blue;
          }
          &.disabled {
            color: #999;
          }
        }
      }
    }
    .main-middle {
      width: 50px;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      i {
        font-size: 18px;
        padding: 16px 0;
        cursor: pointer;
        &:hover {
          color: $project-color-blue;
        }
      }
    }
  }
}
</style>
